home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 September
/
Macworld (1997-09).dmg
/
Serious Software
/
Cherwell Scientific Demos
/
pro Fit
/
pro Fit 5.0 demo (fpu).sea
/
pro Fit 5.0 demo (fpu)
/
Functions & Programs
/
Histogram
< prev
next >
Wrap
Text File
|
1996-04-15
|
4KB
|
128 lines
{ This program generates a histogram of a one-dimensional data set. }
{ The data set (which must be stored in a column of a data window) is }
{ first analyzed by counting the number of data points in each interval. }
{ Then, the number of data points is plotted as a function of the interval. }
{ To use the program, choose "Add to Menu" from the Misc menu to compile it }
{ Then open a data window (if none is open). Then run the program by choosing }
{ "Histogram" from the Misc menu. }
program Histogram;
const maxNrIntervals = 1000;
var window, col:integer; { the window and the column we use }
intervalWidth: real; { the width of an interval }
xMin, xMax: real; { the graph range }
outputTo: integer; { 1: new graph; 2: current graph; 3:new data window }
nrIntervals: integer;
values: array[1..maxNrIntervals];
maxValue: real; { largest value in values }
procedure initialize;
{ this routine is called once when the program is added to }
{ pro Fit's menus }
begin
window := 0; { default: use frontmost data window }
col := 1; { and column 1 }
intervalWidth := 0.01;
xmin := -1;
xmax := 1;
outputTo := 1;
end;
procedure GetUserChoice;
{ ask the user what he/she wants }
begin
Input('$WData window',window, '$CColumn', col,
'X min', xMin, 'X max', xMax,
'Interval width', intervalWidth,
'$Pnew graph;current graph;data window$Ouput to', outputTo);
if intervalWidth <= 0 then
begin
Alert('Interval width must be > 0');
Halt;
end;
if xMin >= xMax then
begin
Alert('X min must be smaller than X max');
Halt;
end;
nrIntervals := (xMax-xMin)/intervalWidth;
if nrIntervals > maxNrIntervals then
begin
Alert('Interval width too small - too many intervals');
Halt;
end;
end;
procedure CalculateValues;
{ fills up the array values[] }
var i, index;
begin
for i := 1 to nrIntervals do values[i] := 0;
for i := 1 to nrRows do
if DataOK(i, col) then { if there is something in this cell }
begin
index := 1 + Round((data[i,col] - xMin)/intervalWidth-0.499999999);
if (index >= 1) and (index <= nrIntervals) then
values[index] := values[index]+1;
end;
maxValue := 0;
for i := 1 to nrIntervals do
if values[i] > maxValue then maxValue := values[i];
end;
procedure Output;
{ creates/draws the graph }
var i, y;
begin
if outputTo = 3 then { if output to data window }
begin
NewWindow(dataType);
SetDataSize(nrIntervals,10);
SetColumnName(1, 'interval center');
SetColumnName(2, 'counts');
for i := 1 to nrIntervals do
begin
data[i,1] := xMin + (i-0.5)*intervalWidth;
data[i,2] := values[i];
end;
Exit;
end;
if outputTo = 1 then { if output to new graph }
begin
SetLineStyle(1,1); { set standard line style, in case it was changed }
SetLineColor(0,0,0); {black}
SetFillColor(65535,0,0); {pure red}
SetCurveFill(xAxis,1);
CreateNewGraph(xMin,xMax,0,maxValue+1,0,0);
SetGraphAttributes(plotBehindAxes+gridInFront);
end;
OpenCurve('Histogram');
MoveTo(xMin,0);
for i := 1 to nrIntervals do
begin
y := values[i];
Line(0, y);
Line(intervalWidth, 0);
Line(0, -y);
end;
CloseCurve;
if outputTo = 1 then {reset some values }
begin
SetCurveFill(xAxis,0);{reset to no filling}
SetFillColor(0,0,0); {black}
SetFillPattern(0);
end;
writeln(nrIntervals);
end;
begin
GetUserChoice;
SetCurrentWindow(window);
CalculateValues;
Output;
end;